<?php
include_once 'index.php';
$ip = '0.0.0.0';
$port = 9501;
$mysql_chain = 20;//数据库重连时间(分钟)
$http = new swoole_http_server($ip, $port);

$http->set(
 array(
  'worker_num' => 15,    //开启worker进程数量
  'max_request' => 200000,//最大任务重启
  'log_level' => 5,//开启报错级别,可以带来性能的提升
  'task_worker_num' => 10
 ));

$http->on("start", function ($http) {
  time_log('系统开始运行可以通过地址访问 http://IP');
});

$http->on("request", function ($resp, $response) use ($http) {
  try {
    \style\longrbl::run($resp, $response, $http);
  } catch (\Exception $e) {
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<pre>" . $e->getMessage() . "</pre>");
  }
});

$http->on('WorkerStart', function ($http, $worker_id) use ($http, $mysql_chain) {

  if ($http->taskworker) {
    $db = new \style\lib\moder($http);
    $db->get();
    $http->tick($mysql_chain * 60000, function () use ($worker_id, $db) {//多少分钟重新连接
      $db->db_task();
    });
  }

});

$http->on('Task', function ($http, $task_id, $src_worker_id, $data) {
  $task_service['task_id'] = $task_id;//当前task进程的id
  $task_service['ser_worker_id'] = $src_worker_id;// 当前任务来着那个worker进程
  try {
    \style\longrbl::task_tun($data, $http, $task_service);
  } catch (Exception $e) {
    time_log($e->getMessage());
  }
});

$http->on('Finish', function ($serv, $task_id, $data) {

});

function time_log($data)
{
  echo '[' . date('Y-m-d h:i:s', time()) . ']->' . $data . PHP_EOL;
}

$http->start();